﻿/*
-----------------------------------------
    Rodeo Game Engine
	Rodeo IO Module
	Copyright(c) Marcin Goryca
-----------------------------------------	
*/
#include "io\log.h"

using namespace rodeocore;
using namespace std;

namespace rodeoio
{
Log::Log(const Log& log)
{
    hfile_ = NULL;
    fileopen_ = false;
}

//.................................//
bool Log::open(const char* fileName)
{
    hfile_ = CreateFile(fileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    
    if(hfile_ == INVALID_HANDLE_VALUE)
    {
        MessageBox(NULL, logFileError, HEADER, errIconOK);
        return false;
    }

    char actualData[68];
    char actualTime[68];

    _strdate_s(actualData);
    _strtime_s(actualTime);

    string getDate = "Log created: ";
    getDate += actualData;
    getDate += ", ";
    getDate += actualTime;

    save(HEADER);
    save(getDate.c_str());

    return true;
}

//...........................................//
void Log::close()
{
    CloseHandle(hfile_);
}

//-------------------------
//void Log::update(const char* filename, const char* buffer)
//{
//
//}
//----------------------------

void Log::write(const char* string)
{
    save(string);
}

//........................................//
void Log::save(const char* string)
{
    //DWORD bytes = 100;
    DWORD bytesWrite = 0;

    const char* newLine = "\r\n";

    WriteFile(hfile_, string, strlen(string), &bytesWrite, NULL);
    WriteFile(hfile_, newLine, strlen(newLine), &bytesWrite, NULL);
}
}    // end of rodeoio namespace